<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>日志管理系统</title>
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
    <link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css" rel="stylesheet">
</head>
<body class="bg-gray-100 min-h-screen">
<div class="container mx-auto px-4 py-8">
    <div class="bg-white rounded-lg shadow-md overflow-hidden max-w-4xl mx-auto">
        <div class="bg-blue-600 text-white px-6 py-4">
            <h1 class="text-2xl font-bold">日志管理系统</h1>
        </div>

        <?php
        // 配置项
        $allowed_dirs = ['app'];
        $selected_dir = $_GET['dir'] ?? 'app';
        $LOG_DIR = __DIR__.'/'.(in_array($selected_dir, $allowed_dirs) ? $selected_dir : 'app').'/';
        define('LOG_DIR', $LOG_DIR);
        define('LOG_PASSWORD', '123450');

        // 确保日志目录存在
        if (!file_exists(LOG_DIR)) {
            mkdir(LOG_DIR, 0755, true);
        }

        // 密码验证
        function verifyPassword() {
            if (!isset($_POST['password'])) return false;
            return $_POST['password'] === LOG_PASSWORD;
        }

        // 获取日志文件列表
        function getLogFiles() {
            $files = [];
            foreach (glob(LOG_DIR.'*.txt') as $file) {
                $files[] = [
                    'name' => basename($file),
                    'size' => filesize($file),
                    'mtime' => date('Y-m-d H:i:s', filemtime($file))
                ];
            }
            usort($files, function($a, $b) {
                return $b['mtime'] <=> $a['mtime'];
            });
            return $files;
        }

        // 读取日志文件
        function readLogFile($filename) {
            $path = LOG_DIR.$filename;
            if (!file_exists($path)) return false;

            return [
                'content' => file_get_contents($path),
                'lines' => count(file($path)),
                'size' => filesize($path)
            ];
        }

        // 删除日志文件
        function deleteLogFile($filename) {
            $path = LOG_DIR.$filename;
            if (file_exists($path)) {
                return unlink($path);
            }
            return false;
        }

        // 主逻辑
        session_start();
        $action = $_GET['action'] ?? 'list';
        $filename = $_GET['file'] ?? '';
        $authenticated = $_SESSION['authenticated'] ?? false;

        // 处理密码提交
        if (isset($_POST['password'])) {
            if (verifyPassword()) {
                $_SESSION['authenticated'] = true;
                $authenticated = true;
            } else {
                $error = "密码错误";
            }
        }

        // 处理删除请求
        if ($authenticated && isset($_GET['delete']) && $filename) {
            if (deleteLogFile($filename)) {
                $message = "日志文件已删除";
                header("Location: ?dir=".$selected_dir."&action=list");
                exit;
            } else {
                $error = "删除失败";
            }
        }

        // 登出处理
        if (isset($_GET['logout'])) {
            session_destroy();
            $authenticated = false;
            header("Location: ?");
            exit;
        }
        ?>

        <div class="p-6">
            <?php if (!$authenticated): ?>
                <!-- 登录表单 -->
                <div class="max-w-md mx-auto bg-white p-8 rounded-lg shadow-md">
                    <h2 class="text-xl font-semibold mb-6 text-center">请输入密码</h2>
                    <?php if (isset($error)): ?>
                        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                            <?php echo $error; ?>
                        </div>
                    <?php endif; ?>
                    <form method="post" class="space-y-4">
                        <div>
                            <input type="password" name="password" placeholder="输入密码"
                                   class="w-full px-4 py-2 border rounded-lg focus:outline-none focus:ring-2 focus:ring-blue-500">
                        </div>
                        <button type="submit"
                                class="w-full bg-blue-600 text-white py-2 px-4 rounded-lg hover:bg-blue-700 transition duration-200">
                            登录
                        </button>
                    </form>
                </div>

            <?php else: ?>
                <!-- 主界面 -->
                <div class="flex justify-between items-center mb-6">
                    <h2 class="text-xl font-semibold">日志文件列表</h2>
                    <div class="flex space-x-2">
                        <?php foreach ($allowed_dirs as $dir): ?>
                            <a href="?dir=<?php echo $dir; ?>"
                               class="px-4 py-2 rounded <?php echo $dir === $selected_dir ? 'bg-blue-500 text-white' : 'bg-gray-200'; ?>">
                                <?php echo $dir; ?>
                            </a>
                        <?php endforeach; ?>
                    </div>
                    <div class="flex space-x-2">
                        <?php if ($action === 'view' && $filename): ?>
                        <a href="?dir=<?php echo $selected_dir; ?>&action=list" class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600 transition duration-200">
                            <i class="fas fa-solid fa-arrow-left mr-2"></i>返回列表
                        </a>
                        <?php endif; ?>
                        <a href="?logout" class="bg-red-500 text-white px-4 py-2 rounded hover:bg-red-600 transition duration-200">
                            <i class="fas fa-sign-out-alt mr-2"></i>登出
                        </a>
                    </div>
                </div>

                <?php if (isset($message)): ?>
                    <div class="bg-green-100 border border-green-400 text-green-700 px-4 py-3 rounded mb-4">
                        <?php echo $message; ?>
                    </div>
                <?php endif; ?>

                <?php if ($action === 'view' && $filename): ?>
                    <?php $log = readLogFile($filename); ?>
                    <?php if ($log !== false): ?>
                        <div class="mb-6">
                            <div class="flex justify-between items-center mb-4">
                                <h3 class="text-lg font-medium"><?php echo htmlspecialchars($filename); ?></h3>
                                <div class="text-sm text-gray-500">
                                    大小: <?php echo round($log['size']/1024, 2); ?> KB |
                                    行数: <?php echo $log['lines']; ?>
                                </div>
                            </div>
                            <div class="bg-gray-50 p-4 rounded border">
                                <pre class="whitespace-pre-wrap"><?php echo htmlspecialchars($log['content']); ?></pre>
                            </div>
                            <div class="mt-4">
                                <a href="?dir=<?php echo $selected_dir; ?>&action=list" class="text-blue-600 hover:underline">
                                    <i class="fas fa-arrow-left mr-1"></i> 返回列表
                                </a>
                            </div>
                        </div>
                    <?php else: ?>
                        <div class="bg-red-100 border border-red-400 text-red-700 px-4 py-3 rounded mb-4">
                            文件不存在或无法读取
                        </div>
                        <a href="?dir=<?php echo $selected_dir; ?>&action=list" class="text-blue-600 hover:underline">
                            <i class="fas fa-arrow-left mr-1"></i> 返回列表
                        </a>
                    <?php endif; ?>

                <?php else: ?>
                    <!-- 文件列表 -->
                    <div class="overflow-x-auto">
                        <table class="min-w-full bg-white">
                            <thead>
                            <tr class="bg-gray-100">
                                <th class="py-3 px-4 text-left">文件名</th>
                                <th class="py-3 px-4 text-left">大小</th>
                                <th class="py-3 px-4 text-left">修改时间</th>
                                <th class="py-3 px-4 text-left">操作</th>
                            </tr>
                            </thead>
                            <tbody>
                            <?php $files = getLogFiles(); ?>
                            <?php if (count($files) > 0): ?>
                                <?php foreach ($files as $file): ?>
                                    <tr class="border-t hover:bg-gray-50">
                                        <td class="py-3 px-4">
                                            <a href="?dir=<?php echo $selected_dir; ?>&action=view&file=<?php echo urlencode($file['name']); ?>"
                                               class="text-blue-600 hover:underline">
                                                <?php echo htmlspecialchars($file['name']); ?>
                                            </a>
                                        </td>
                                        <td class="py-3 px-4"><?php echo round($file['size']/1024, 2); ?> KB</td>
                                        <td class="py-3 px-4"><?php echo $file['mtime']; ?></td>
                                        <td class="py-3 px-4">
                                            <div class="flex space-x-2">
                                                <a href="?dir=<?php echo $selected_dir; ?>&action=view&file=<?php echo urlencode($file['name']); ?>"
                                                   class="text-blue-600 hover:text-blue-800">
                                                    <i class="fas fa-eye"></i>
                                                </a>
                                                <a href="?dir=<?php echo $selected_dir; ?>&action=list&delete=1&file=<?php echo urlencode($file['name']); ?>"
                                                   class="text-red-600 hover:text-red-800"
                                                   onclick="return confirm('确定要删除此文件吗？');">
                                                    <i class="fas fa-trash-alt"></i>
                                                </a>
                                            </div>
                                        </td>
                                    </tr>
                                <?php endforeach; ?>
                            <?php else: ?>
                                <tr class="border-t">
                                    <td colspan="4" class="py-4 px-4 text-center text-gray-500">
                                        没有找到日志文件
                                    </td>
                                </tr>
                            <?php endif; ?>
                            </tbody>
                        </table>
                    </div>
                <?php endif; ?>
            <?php endif; ?>
        </div>
    </div>
</div>
</body>
</html>
